package org.yzh.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.yzh.web.component.mybatis.Page;
import org.yzh.web.component.mybatis.PageInfo;
import org.yzh.web.component.mybatis.Pagination;
import org.yzh.web.component.qiniu.QiniuManager;
import org.yzh.web.model.APIResult;
import org.yzh.web.model.vo.*;
import org.yzh.web.service.DeviceService;
import org.yzh.web.service.LocationService;

import java.util.List;

/**
 * @author lsj
 * @date 2021/1/13 21:16
 */
@RestController
public class DeviceController {

    @Autowired
    private LocationService locationService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private QiniuManager qiniuManager;

    @Operation(summary = "位置信息查询")
    @GetMapping("locations")
    public APIResult<Pagination<Location>> find(LocationQuery query, PageInfo pageInfo) {
        return APIResult.success((Pagination<Location>)Page.start(() -> locationService.find(query), pageInfo));
    }

    @Operation(summary = "全部设备最后位置信息")
    @GetMapping("locations/lasts")
    public APIResult<List<DeviceLastLocation>> findDeviceLastLocation(LocationQuery query) {
        return APIResult.success(locationService.findLastLocation(query));
    }

    @Operation(summary = "设备最后位置详情")
    @GetMapping("locations/lasts/detail")
    public APIResult<DeviceLastLocationDetail> findDeviceLastLocationDetail(String plateNo) {
        return APIResult.success(locationService.findDeviceLastLocationDetail(plateNo));
    }

    @Operation(summary = "设备状态统计")
    @GetMapping("devices/status/counts")
    public APIResult<DeviceStatusCount> findDeviceStatusCount() {
        return APIResult.success(deviceService.findDeviceStatusCount());
    }

    @Operation(summary = "获取动态推流地址")
    @GetMapping("devices/qiniu_dynamic_url")
    public APIResult<QiniuDynamicUrl> getQiniuDynamicUrl(String deviceId) {
        return APIResult.success(qiniuManager.generateDynamicUrl(deviceId));
    }
}
